from pymysql import Connection

# 获取到MYSQL的连接对象
conn = Connection(
    host="localhost",
    port=3306,
    user="root",
    password="",
    autocommit=True  # 设置自动提交
)
# 打印MYSQL相关信息
# print(conn.get_server_info())


# 使用游标执行sql语句
cursor = conn.cursor()
conn.select_db("db02")  # 选择数据库

# 执行DDL
'''
cursor.execute("create table test_pysql(id int not null , info varchar(30), primary key(id))")
'''

# 执行DML

cursor.execute("select * from book")
results = cursor.fetchall()  # ((1, '查理九世', '雷欧'), (2, '小王子', '安徒生'))
print(results)
for i in results:
    print(i)


# 插入操作
'''
cursor.execute("insert into book values(3,'我与地坛','史铁生')")
conn.commit()  # 对数据库的修改必须有确认操作
'''


# 关闭数据库连接
conn.close()
